<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/21
 * Time: 11:32
 */
namespace app\index\controller;

use think\Controller;
use think\Db;

class Importdata extends Controller {

	public function updateUser(){
		set_time_limit(0);
		$users = Db::name('user')
			->where('loginip > 0 and recommend_id is null')
			->field('id, mobile, joinip, loginip')
			->select();
		
		foreach ($users as $user) {
			$data = [];
			$rec = Db::name('user')
				->where(['mobile' => $user['loginip']])
				->field('id, mobile')
				->find();
			$rec && $data['recommend_id'] = $rec['id'];
			
			$agent = Db::name('user')
				->where(['mobile' => $user['joinip']])
				->field('id, mobile')
				->find();
			$agent && $data['agent_user_id'] = $agent['id'];
			if($data) {
				Db::name('user')
					->where(['id' => $user['id']])
					->update($data);
			}
			
		}
		echo 'success';
	}
	
	public function uploadFunds(){
		
		set_time_limit(0);
		$rows = Db::name('fundsRecord')->alias('r')
			->join('user u', 'r.user_id = u.mobile','left')
			->join('user rl', 'r.relation_user_id = rl.mobile','left')
			->field('r.id, u.id as uid, rl.id as rid')
			->select();
		
		foreach ($rows as $row) {
			Db::name('fundsRecord')
				->where(['id' => $row['id']])
				->update(['user_id' => $row['uid'], 'relation_user_id' => $row['rid']]);
		}
		
		echo 'success';
	}
}